//
//  ViewController.m
//  CollectionView轮播图
//
//  Created by dllo on 16/8/4.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "SecCollectionViewCell.h"
#import "YHCycleImage.h"

@interface ViewController ()
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *picArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.picArray = [NSMutableArray array];
    for (NSInteger i = 1; i < 8; i++) {
        UIImage *image = [UIImage imageNamed:[@(i) description]];
        [self.picArray addObject:image];
    }
    YHCycleImage *cycle = [[YHCycleImage alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 270)];
    [cycle createPicArray:self.picArray];
    [self.view addSubview:cycle];
    
    
    
//    [self createCollectionView];
//    [self dealWithImage];
}
#if 0
- (void)dealWithImage {
    self.picArray = [NSMutableArray array];
    for (NSInteger i = 1; i < 8; i++) {
        UIImage *image = [UIImage imageNamed:[@(i) description]];
        [self.picArray addObject:image];
    }
    [self.picArray addObject:[UIImage imageNamed:[@(1) description]]];
    [self.picArray insertObject:[UIImage imageNamed:[@(7) description]] atIndex:0];
    
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
// 写了这个方法可以让collectionVIew在滑动时每一张图片都显示完全 不留空隙 (这个方法是UICollectionViewDelegateFlowLayout协议的)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.bounds.size.width, 400);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 400) collectionViewLayout:layout];
    self.collectionView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"SecCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"reuse"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SecCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
   
//    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 256.0 green:arc4random() % 255 / 256.0 blue:arc4random() % 255 / 256.0 alpha:1];
    cell.picImageView.image = self.picArray[indexPath.row];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.collectionView.contentOffset.x == self.view.bounds.size.width * 8) {
        self.collectionView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    }
    if (self.collectionView.contentOffset.x == 0) {
        self.collectionView.contentOffset = CGPointMake(self.view.frame.size.width * 7, 0);
    }
}
#endif
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
